home *** CD-ROM | disk | FTP | other *** search
/ Hyper Stacks 1994 May / Hyper Stacks (Pacific HiTech)(1994)[Mac].iso / MacTools / TC Prog Guide / app ƒ / testapp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-08  |  6.4 KB  |  227 lines  |  [TEXT/KAHL]

  1. /*
  2. *    FILE:        testapp.c
  3. *    AUTHOR:        R. Gonzalez
  4. *    CREATED:    August 25, 1990
  5. *
  6. *    Defines simple "database" application commands, main function.
  7. *
  8. *    ASSOCIATED FILES:
  9. *        app.c,app.h,class.c,class.h,comapp.c,comapp.h,comline.c,
  10. *        comline.h,macapp.c,macapp.h,menapp.c,menapp.h,menu.c,menu.h,
  11. *        menubar.c,menubar.h,morestr.c,morestr.h,dfile.c,dfile.h,
  12. *        testapp.h.
  13. *
  14. *    PROJECT CONTENTS (Macintosh):
  15. *        above-listed source (.c) files, MacTraps, ANSI or ANSI-881,
  16. *        oops library.
  17. *
  18. *    COMPILATION (Macintosh):
  19. *        include precompiled header MacHeaders, 68881 code generation
  20. *        if ANSI-881 is used.
  21. */
  22.  
  23. # ifdef        THINK_C
  24. # include    <oops.h>
  25. # endif
  26.  
  27. # include    <string.h>
  28. # include    "testapp.h"
  29.  
  30. # define    MAX_LENGTH    80
  31.  
  32. static boolean    newfil(Test_App*);
  33. static boolean    oopen(Test_App*);
  34. static boolean    save(Test_App*);
  35. static boolean    quit(Test_App*);
  36. static boolean    enter_data(Test_App*);
  37. static boolean    list_data(Test_App*);
  38. static boolean    sort_by_name(Test_App*);
  39. static boolean    sort_by_age(Test_App*);
  40. static boolean    report_filename(Test_App*);
  41.  
  42. /************************************************************************
  43. *    initialize application
  44. ************************************************************************/
  45. boolean    Test_App::init(void)
  46. {
  47.     PARENT_CLASS::init();    /* don't forget to call this! */
  48.     
  49.     menu->log_command(1,0,"File",NULL);
  50.     menu->log_command(1,1,"New",(comfunc) newfil);
  51.     menu->log_command(1,2,"Open",(comfunc) oopen);
  52.     menu->log_command(1,3,"Save",(comfunc) save);
  53.     menu->log_command(1,4,"Quit",(comfunc) quit);
  54.     menu->log_command(2,0,"Data",NULL);
  55.     menu->log_command(2,1,"Enter",(comfunc) enter_data);
  56.     menu->log_command(2,2,"List",(comfunc) list_data);
  57.     menu->log_command(3,0,"Options",NULL);
  58.     menu->log_command(3,1,"SortByName",(comfunc) sort_by_name);
  59.     menu->log_command(3,2,"SortByAge",(comfunc) sort_by_age);
  60.     menu->log_command(3,3,"ReportFilename",(comfunc) report_filename);
  61.  
  62.     menu->log_command(1,5,"Bye",(comfunc) quit);
  63.     menu->log_command(1,6,"Goodbye",(comfunc) quit);
  64.     menu->log_command(1,7,"Stop",(comfunc) quit);
  65.     menu->log_command(1,8,"Logout",(comfunc) quit);
  66.     menu->log_command(1,9,"Logoff",(comfunc) quit);
  67.     menu->log_command(1,10,"Exit",(comfunc) quit);
  68.     menu->log_command(1,11,"Ciao",(comfunc) quit);
  69.     menu->log_command(1,12,"AufWiedersehen",(comfunc) quit);
  70.     menu->log_command(1,13,"AuRevoir",(comfunc) quit);
  71.     menu->log_command(1,14,"SeeYouLaterAlligator",(comfunc) quit);
  72.  
  73.     dfile = new(Dfile);
  74.     dfile->init();
  75.     
  76.     return TRUE;
  77. }
  78.  
  79. /************************************************************************
  80. *    terminate application - free up allocated space.
  81. ************************************************************************/
  82. boolean    Test_App::destroy(void)
  83. {
  84.     dfile->destroy();
  85.     delete(dfile);
  86.     
  87.     return PARENT_CLASS::destroy();
  88. }
  89.  
  90. /*------------------------ command functions --------------------------*/
  91.  
  92. /************************************************************************
  93. *    clear database memory.
  94. ************************************************************************/
  95. static boolean    newfil(Test_App *app_ptr)
  96. {
  97.     app_ptr->dfile->destroy();
  98.     app_ptr->dfile->init();
  99.     
  100.     return TRUE;
  101. }
  102.  
  103. /************************************************************************
  104. *    open record file.  For some reason there was a conflict with the
  105. *    name "open" so I used "oopen".
  106. ************************************************************************/
  107. static boolean    oopen(Test_App *app_ptr)
  108. {
  109.     char    temp_char[MAX_LENGTH];
  110.  
  111.     app_ptr->query("Enter file name: ",temp_char);
  112.     
  113.     if (!app_ptr->dfile->open_existing(temp_char))
  114.         app_ptr->respond("No such file.");
  115.     
  116.     return TRUE;
  117. }
  118.  
  119. /************************************************************************
  120. *    save records.
  121. ************************************************************************/
  122. static boolean    save(Test_App *app_ptr)
  123. {
  124.     char    temp_char[MAX_LENGTH];
  125.     
  126.     app_ptr->query("Enter file name (RETURN for previous): ",temp_char);
  127.     
  128.     if (!app_ptr->dfile->write_valid(temp_char))
  129.         app_ptr->respond("Can't open file.");
  130.     
  131.     return TRUE;
  132. }
  133.  
  134. /************************************************************************
  135. *    quit.
  136. ************************************************************************/
  137. boolean    quit(Test_App *app_ptr)
  138. {
  139.     app_ptr->done = TRUE;
  140.     return TRUE;
  141. }
  142.  
  143. /************************************************************************
  144. *    enter data record.
  145. ************************************************************************/
  146. static boolean    enter_data(Test_App *app_ptr)
  147. {
  148.     boolean    success;
  149.     char    *question1 = "Enter your name: ",
  150.             *question2 = "Enter your age: ",
  151.             answer1[MAX_LENGTH],
  152.             answer2[MAX_LENGTH];
  153.     
  154.     app_ptr->query(question1,answer1);
  155.     app_ptr->query(question2,answer2);
  156.     
  157.     if (strcmp(answer1,"") != 0 || strcmp(answer2,"") != 0)
  158.     {
  159.         success = TRUE;
  160.         if (!app_ptr->dfile->add_record(answer1,answer2))
  161.             app_ptr->respond("Too many records.");
  162.     }
  163.     else
  164.         success = FALSE;
  165.     
  166.     return success;
  167. }
  168.  
  169. /************************************************************************
  170. *    list data records.
  171. ************************************************************************/
  172. static boolean    list_data(Test_App *app_ptr)
  173. {
  174.     int        i=0;
  175.     char    response[MAX_LENGTH];
  176.     
  177.     while (app_ptr->dfile->get_record(i++,response))
  178.         app_ptr->respond(response);
  179.         
  180.     if (i == 1)
  181.         app_ptr->respond("No entries.");
  182.     
  183.     return TRUE;
  184. }
  185.  
  186. /************************************************************************
  187. *    sort by name.
  188. ************************************************************************/
  189. static boolean    sort_by_name(Test_App *app_ptr)
  190. {
  191.     return app_ptr->dfile->sort_by_name();
  192. }
  193.  
  194. /************************************************************************
  195. *    sort by age.
  196. ************************************************************************/
  197. static boolean    sort_by_age(Test_App *app_ptr)
  198. {
  199.     return app_ptr->dfile->sort_by_age();
  200. }
  201.  
  202. /************************************************************************
  203. *    report filename.  This is just included to demonstrate how to access
  204. *    query() and respond() from other objects.
  205. ************************************************************************/
  206. static boolean    report_filename(Test_App *app_ptr)
  207. {
  208.     app_ptr->dfile->show_name(app_ptr);
  209.     
  210.     return TRUE;
  211. }
  212.  
  213. /************************************************************************
  214. *    main - you found it!
  215. ************************************************************************/
  216. main()
  217. {
  218.     Generic_App        *application;
  219.     
  220.     application = new(Test_App);
  221.     application->init();
  222.     application->run();
  223.     application->destroy();
  224.     delete(application);
  225. }
  226.  
  227.